package com.yongcheon.sms_manager.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.receiver.sms.SmsReceiver;
import com.yongcheon.sms_manager.utils.AndroidSystemUtil;
import com.yongcheon.sms_manager.utils.DefaultAppUtil;
import com.yongcheon.sms_manager.vo.SMSVO;

/**
 * Created by yongcheon on 2014-12-18.
 */
public class NormalNotification {
    public void setNotification(Context context, SMSVO smsVO) {
        NotificationManager mNotiManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

        Intent intent = new Intent();
        intent.setPackage(DefaultAppUtil.getDefaultAppPkgName(context));
        intent.setAction(Intent.ACTION_MAIN);
        PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), SmsReceiver.NOTI_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT );

        if(AndroidSystemUtil.ANDROID_VERSION < 16) {
            setNoti15(context, smsVO, mNotiManager, pIntent);
        }else{
            setNoti16(context, smsVO, mNotiManager, pIntent);
        }
    }

    public void setNoti16(Context context, SMSVO smsVO, NotificationManager mNotiManager, PendingIntent pIntent){
        Notification.Builder notiBuilder = new Notification.Builder(context.getApplicationContext())
                .setSmallIcon(R.drawable.ic_launcher_1)
                .setContentTitle(context.getResources().getString(R.string.sms_arrive))
                .setPriority(1)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentIntent(pIntent)
                .setTicker(context.getResources().getString(R.string.sms_arrive));

        Notification.InboxStyle inboxStyle = new Notification.InboxStyle(notiBuilder);
        inboxStyle.addLine(smsVO.getAddress());
        inboxStyle.addLine(smsVO.getBody());
        Notification noti = inboxStyle.build();
        noti.flags |= Notification.FLAG_ONGOING_EVENT;
        noti.flags |= Notification.FLAG_NO_CLEAR;

        mNotiManager.notify(SmsReceiver.NOTI_ID, noti);
    }

    public void setNoti15(Context context, SMSVO smsVO, NotificationManager mNotiManager, PendingIntent pIntent){
        String contextText = smsVO.getAddress();
        Notification notification = new Notification(R.drawable.ic_launcher_smsmms, context.getResources().getString(R.string.sms_arrive), System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
        notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE ;
        notification.setLatestEventInfo(context, contextText, smsVO.getBody(), null);
        mNotiManager.notify(SmsReceiver.NOTI_ID, notification);
    }
}
